import tkinter as tk
from tkinter import ttk

'''
窗口实例，自动居中，管理窗口最大、最小或尺寸固定
'''
class main_frame():
    def __init__(self):
        self.win = tk.Tk()
        self.win.title("窗口实例")  
        self.center_window( 800, 600)
        label1 = tk.Label(self.win)
        label1.configure(text="任意键关闭窗口")
        label1.pack(expand=True)


        self.win.bind("<Key>", self.myquit)
        self.win.mainloop() 

    def center_window(self, width, height):
        '''
        窗口居中
        '''
        screenwidth = self.win.winfo_screenwidth()
        screenheight = self.win.winfo_screenheight()
        size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 3)
        self.win.geometry(size)  #初始化宽高和位置
        self.win.minsize(width, height) # 最小宽高
        #self.win.resizable(width=False, height=False)#禁止改变窗口大小
        self.win.attributes("-fullscreen", True)  #窗口最大化
        self.win.update()

    #-------关闭窗口--------
    def myquit(self,event):
        '''
        任意键关闭窗口
        '''
        #英文输入模型下有效
        print(event.char)
        self.win.destroy()

main_frame()